package com.wan37.logic.handler.message;

import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.entity.SceneDO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.MonsterVO;
import com.wan37.logic.global.HandlerManager;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.service.cache.MonsterServiceCache;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.team.cache.TeamCache;
import com.wan37.logic.team.domain.vo.TeamVO;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.PtoNum;

import com.wan37.protobuf.message.client2server.game.GameBossScene;
import io.netty.util.internal.ConcurrentSet;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author : luoyong
 * @date : 2020-06-30 16:12
 **/
public class CGameBossSceneHandler extends MsgHandler {
    private SceneServiceCache sceneServiceCache;
    private MonsterServiceCache monsterServiceCache;
    private UserServiceCache userServiceCache;
    private RoleServiceCache roleServiceCache;
    private RouteUtils routeUtils;
    private TeamCache teamCache;

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameBossSceneHandler(MessageVO messageVO) {
        super(messageVO);
        this.sceneServiceCache = SpringContextHolder.getBean("sceneServiceCache");
        this.monsterServiceCache = SpringContextHolder.getBean("monsterServiceCache");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
        this.roleServiceCache = SpringContextHolder.getBean("roleServiceCache");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.teamCache = SpringContextHolder.getBean("teamCache");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        /**
         * 先进行组队逻辑处理
         */
        String teamName = userServiceCache.getUserServiceMap().get(messageVO.getUserId()).getTeamName();
        if (teamName == null || "".equals(teamName)){
            List<String> userIdList = new ArrayList<>();
            userIdList.add(messageVO.getUserId());
            String bossSceneId = createBossScene(userIdList);
            /**
             * 返回场景构建成功消息
             */
            routeUtils.sendResponse(Common.GAME_BOSS_SCENE_SUCCESS, bossSceneId, 1, messageVO.getUserId());
        }else {
            TeamVO teamVO = teamCache.getTeamInMap().get(teamName).getTeamVO();
            if (!teamVO.getCaptain().equals(messageVO.getUserId())){
                routeUtils.sendResponse(Common.GAME_MOVE_ERROR, "你不是队长，不允许刷怪！", 1, messageVO.getUserId());
            }else {
                String bossSceneId = createBossScene(teamVO.getUserIdList());
                for (String userId : teamVO.getUserIdList()){
                    /**
                     * 返回场景构建成功消息
                     */
                    routeUtils.sendResponse(Common.GAME_BOSS_SCENE_SUCCESS, bossSceneId, 1, userId);
                }
            }
        }
    }

    public String createBossScene(List<String> userIdList) throws Exception {
        GameBossScene.CGameBossSceneMsg cGameBossScene = (GameBossScene.CGameBossSceneMsg) messageVO.getMessage();
        String bossSceneId = cGameBossScene.getBossSceneId();
        String preSceneId = sceneServiceCache.sceneNameToIdMap.get(cGameBossScene.getOldSceneId());
        /**
         * 构建一个临时场景，完善数据并且插入场景缓存中
         */
        SceneServiceCache.Scene scene = sceneServiceCache.new Scene();
        SceneDO sceneDO = new SceneDO();
        sceneDO.setSceneId(bossSceneId);
        scene.setSceneDO(sceneDO);
        scene.setCreatTime(System.currentTimeMillis());
        scene.setPreSceneId(preSceneId);
        //0 永久场景  1 临时场景
        scene.setLabel(1);
        scene.setAllRoleVOMap(new ConcurrentHashMap<>());
        scene.setUserIdSet(new ConcurrentSet<>());
        scene.setGoSceneIdSet(new ConcurrentSet<>());
        cGameBossScene.getMonsterSList().forEach(stringMsg -> {
            MonsterVO monsterVO = monsterServiceCache.getMonsterServiceMap().get(stringMsg.getStringId()).getMonsterVO();
            MonsterVO bossVO = new MonsterVO();
            bossVO.setSceneId(bossSceneId);
            bossVO.setAllRoleId(monsterVO.getAllRoleId());
            bossVO.setLabel(monsterVO.getLabel());
            bossVO.setName(monsterVO.getName());
            bossVO.setStatus(monsterVO.getStatus());
            bossVO.setNumberMap(new ConcurrentHashMap<>());
            monsterVO.getNumberMap().forEach((s, l) -> {
                bossVO.getNumberMap().put(s, l);
            });
            scene.getAllRoleVOMap().put(bossVO.getAllRoleId(), bossVO);
        });
        sceneServiceCache.sceneNameToIdMap.put(bossSceneId, bossSceneId);
        sceneServiceCache.sceneSet.add(bossSceneId);

        scene.getGoSceneIdSet().add(preSceneId);
        sceneServiceCache.getSceneServiceMap().put(bossSceneId, scene);
        sceneServiceCache.getSceneServiceMap().get(preSceneId).getGoSceneIdSet().add(bossSceneId);

        CGameMoveHandler cGameMoveHandler = (CGameMoveHandler) HandlerManager.getMsgHandler
                (PtoNum.CGAMEMOVE, messageVO.getUserId(), messageVO.getNetId(), null, messageVO.getCtx());
        for (String userId : userIdList){
            cGameMoveHandler.move(preSceneId, bossSceneId, userId, bossSceneId);
            scene.getUserIdSet().add(userId);
        }
        return bossSceneId;
    }
}
